package com.rsy.crm.web.filter;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 登陆验证过滤器
 */

public  class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("登陆验证过滤器");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        //从session中获取uesr
        HttpSession session =request.getSession();
        Object user = session.getAttribute("user");

        String path = request.getServletPath();
        System.out.println(path);
        /*||(".JPG".equals(path))*/
        if(("/login.jsp".equals(path))||("/settings/user/login.do".equals(path))||(path.indexOf(".JPG")!= -1)||(path.indexOf(".css")!= -1)||(path.indexOf(".js")!= -1)||(session != null&&user!=null)){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }
    }
}
